import sys, re, wx



class EasyListCtrl(wx.ListCtrl):

#    def __init__(self, parent, id, pos, size, style, validator, name):
#        wx.ListCtrl.__init__(parent, id, pos, size, style, validator, name)
#        self.DeleteAllItems()

    def __len__(self):
        return self.GetItemCount()

    def AppendColumns(self, schema):
        self._id2col = {}
        for col, item in enumerate(schema):
            self.InsertColumn(col, item['heading'], width=item['width'])
            self._id2col[item['id']] = col

    def AppendRow(self, row):
        return self.InsertRow(sys.maxint, row)

    def InsertRow(self, index, row):
        newIndex = self.InsertStringItem(index, '')
        self.UpdateRow(newIndex, row)
        return newIndex

    def UpdateRow(self, index, row):
        # id: user identifier for the column
        # col: position of the column in the row
        for id, col in self._id2col.iteritems():
            if id in row and row[id] is not None:
                self.SetStringItem(index, col, row[id])

    def GetRow(self, index):
        if index < 0 or index >= self.GetItemCount():
            return None
        row = {}        
        for id, col in self._id2col.iteritems():
            row[id] = self.GetItem(index, col).GetText()
        return row

    def SelectAllRows(self, on=True):
        index = self.GetNextItem(-1)
        while index != -1:
            self.Select(index, on)
            index = self.GetNextItem(index)

    def GetSelectedIndexes(self):
        selectedIndexes = []
        index = self.GetFirstSelected()
        while (index != -1):
            selectedIndexes.append(index)
            index = self.GetNextSelected(index)
        return selectedIndexes

    def GetAllRows(self):
        allRows = []
        index = self.GetNextItem(-1)
        while index != -1:
            allRows.append(self.GetRow(index))
            index = self.GetNextItem(index)
        return allRows

    def GetSelectedRows(self):
        selectedRows = []
        index = self.GetFirstSelected()
        while (index != -1):
            selectedRows.append(self.GetRow(index))
            index = self.GetNextSelected(index)
        return selectedRows

    def DeleteSelectedRows(self):
        selectedRows = []
        index = self.GetFirstSelected()
        while (index != -1):
            selectedRows.append(self.GetRow(index))
            self.DeleteItem(index)
            index = self.GetFirstSelected()
        return selectedRows

    def InsertRowInOrder(self, row, col):
        index = self.GetNextItem(-1)
        inserted = False
        while index != -1 and not inserted:
            if row[col] < self.GetRow(index)[col]:
                newIndex = self.InsertRow(index, row)
                inserted = True
            index = self.GetNextItem(index)
        if not inserted:
            newIndex = self.AppendRow(row)
        return newIndex

    def MoveUpSelectedRow(self):
        index = self.GetFirstSelected()
        if (index != -1 and index > 0):
            row = self.GetRow(index)
            self.DeleteItem(index)
            newIndex = self.InsertRow(index-1, row)
            self.SetItemState(newIndex, wx.LIST_STATE_SELECTED, wx.LIST_STATE_SELECTED)

    def MoveDownSelectedRow(self):
        index = self.GetFirstSelected()
        if (index != -1 and index < self.GetItemCount()-1):
            row = self.GetRow(index)
            self.DeleteItem(index)
            newIndex = self.InsertRow(index+1, row)
            self.SetItemState(newIndex, wx.LIST_STATE_SELECTED, wx.LIST_STATE_SELECTED)

    def GetAllRows(self):
        rows = []
        index = self.GetNextItem(-1)
        while index != -1:
            rows.append(self.GetRow(index))
            index = self.GetNextItem(index)
        return rows



